package com.xin.core
{
import com.xin.components.base.LiteSprite;
import com.xin.utils.TipUtils;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;

public class Game
{
	private var _scene:IScene;
	private var _stage:Stage;
	
	private var _tipLayer:LiteSprite;
	private var _windowLayer:LiteSprite;
	private var _uiLayer:LiteSprite;
	
	public function Game(stage:Stage, w:int, h:int, uiLayer:LiteSprite)
	{
		_stage = stage;
		_stage.align = StageAlign.TOP_LEFT;
		_stage.scaleMode = StageScaleMode.NO_SCALE;
		_stage.quality = StageQuality.BEST;
		_stage.showDefaultContextMenu = false;
		
		_uiLayer = uiLayer;
		_stage.addChild(_uiLayer);
		
		_windowLayer = new LiteSprite(w, h);
		_stage.addChild(_windowLayer);
		
		_tipLayer = new LiteSprite(w, h);
		_stage.addChild(_tipLayer);
		
		TipUtils.getInstance().init(_tipLayer);
	}
	
	/**
	 * 显示场景
	 */
	public function showScene(scene:IScene):void
	{
		if (scene == null) return;
		
		//退出原场景
		if (_scene != null)
		{
			_stage.removeChild(_scene as DisplayObject);
			_scene.onExit();
			_scene = null;
		}
		
		//清空tip和对话框
		while (_tipLayer.numChildren)
		{
			_tipLayer.removeChildAt(0);
		}
		while (_windowLayer.numChildren)
		{
			_windowLayer.removeChildAt(0);
		}
		
		//显示新场景
		_scene = scene;
		_stage.addChildAt(_scene as DisplayObject, 0);
		_scene.onEnter();
	}
	
	public function get windowLayer():Sprite
	{
		return _windowLayer;
	}
	
	public function get tipLayer():Sprite
	{
		return _tipLayer;
	}
	
	public function get uiLayer():Sprite
	{
		return _uiLayer;
	}
}
}